package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private  IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    /**
     * 跳转修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/input")
    public String input(Model model,Long id) {

        //strategies
        model.addAttribute("strategies",strategyService.list());
        //themes
        model.addAttribute("themes",strategyThemeService.list());

        //strategyDetail
        if (id != null) {
            StrategyDetail strategyDetail = strategyDetailService.get(id);
            //获取文章内容
            strategyDetail.setStrategyContent(strategyDetailService.getContent(id));
            model.addAttribute("strategyDetail", strategyDetail);
            //获取游记标签
            String tags = strategyTagService.getRelationTagByDetailId(id);
            model.addAttribute("tags", tags);
        }

        return "strategyDetail/input";
    }

    /**
     * 二级联动
     * @param strategyId
     * @return
     */
    //getCatalogByStrategyId
    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId) {

        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.getCatalogByStrategyId(strategyId);

        return strategyCatalogs;
    }


    /**
     * 保存或修改
     * @param strategyDetail
     * @param tags
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail, String tags) {

        strategyDetailService.saveOrUpdate(strategyDetail,tags);

        return AjaxResult.success();
    }

    //删除操作
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        strategyDetailService.deleteById(id);
        return AjaxResult.success();
    }

    //修改状态
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Long state, Long id) {
        strategyDetailService.changeState(id, state);
        return AjaxResult.success();
    }
}
